package com.ggh.jinjilive.ui.mine.zhubo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentZhuboStatementBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mine.zhubo.ZhuBoStatementMobile;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mine.zhubo.adapter.ZhuBoStatementAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;

public class ZhuBoStatementFragment extends BaseFragment<FragmentZhuboStatementBinding> implements AddItemOnClickListener,View.OnClickListener{
    private ZhuBoViewModel zhuBoViewModel;
    //适配器
    private ZhuBoStatementAdapter zhuBoStatementAdapter;
    //日期
    private String date;
    //报表
    private int statement;
    public ZhuBoStatementFragment(int statement){
        this.statement = statement;

    }


    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        zhuBoViewModel = new ViewModelProvider(this,viewModelFactory).get(ZhuBoViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_zhubo_statement;
    }

    @Override
    public void initView() {
        super.initView();
        zhuBoStatementAdapter = new ZhuBoStatementAdapter();
        zhuBoStatementAdapter.setAddItemOnClickListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        binding.dayListView.setLayoutManager(linearLayoutManager);
        binding.dayListView.setAdapter(zhuBoStatementAdapter);
        if(statement == 0){
            binding.llDay.setVisibility(View.VISIBLE);
            binding.llWeeks.setVisibility(View.GONE);
            binding.llMonth.setVisibility(View.GONE);
        }else if(statement == 1){
            binding.llDay.setVisibility(View.GONE);
            binding.llWeeks.setVisibility(View.VISIBLE);
            binding.llMonth.setVisibility(View.GONE);
        }else if(statement == 2){
            binding.llDay.setVisibility(View.GONE);
            binding.llWeeks.setVisibility(View.GONE);
            binding.llMonth.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initData() {
        super.initData();
        //主播报表
        zhuBoViewModel.AnchorAuthReport();

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //主播报表
        zhuBoViewModel.mZhuBoStatementMobile.observe(this, new Observer<ZhuBoStatementMobile>() {
            @Override
            public void onChanged(ZhuBoStatementMobile zhuBoStatementMobile) {
                if(null != zhuBoStatementMobile){

                    if (zhuBoStatementMobile.getBankcard().equals(" **** **** *** ")){
                        binding.bankAccount.setText("未绑定");
                    }else {
                        binding.bankAccount.setText("已绑定");
                    }
                    binding.idCard.setText(zhuBoStatementMobile.getIs_anchor_auth() == 1?"已上传":"未上传");
                    if(statement == 0){
                        zhuBoStatementAdapter.setAdd(zhuBoStatementMobile.getDay());
                        if(null != zhuBoStatementMobile.getDay() && zhuBoStatementMobile.getDay().size() > 0){
                            zhuBoViewModel.AnchorDayReport(zhuBoStatementMobile.getDay().get(0));
                            date = zhuBoStatementMobile.getDay().get(0);
                        }
                    }else if(statement == 1){
                        zhuBoStatementAdapter.setAdd(zhuBoStatementMobile.getWeek());
                        if(null != zhuBoStatementMobile.getWeek() && zhuBoStatementMobile.getWeek().size() > 0){
                            zhuBoViewModel.AnchorWeekReport(zhuBoStatementMobile.getWeek().get(0));
                            date = zhuBoStatementMobile.getWeek().get(0);
                        }
                    }else if(statement == 2){
                        zhuBoStatementAdapter.setAdd(zhuBoStatementMobile.getMonth());
                        if(null != zhuBoStatementMobile.getMonth() && zhuBoStatementMobile.getMonth().size() > 0){
                            zhuBoViewModel.AnchorMonyhReport(zhuBoStatementMobile.getMonth().get(0));
                            date = zhuBoStatementMobile.getMonth().get(0);
                        }
                    }

                }
            }
        });
        //日期报表
        zhuBoViewModel.mDateZhuBoStatementMobile.observe(this, new Observer<ZhuBoStatementMobile>() {
            @Override
            public void onChanged(ZhuBoStatementMobile zhuBoStatementMobile) {
                if(null != zhuBoStatementMobile){
                    if(statement == 0){//日
                        binding.dailyLiveTimeText1.setText(zhuBoStatementMobile.getDay_live_time());
                        binding.dailyVideoPostText1.setText(zhuBoStatementMobile.getDay_video_num() + "条");
                        binding. profitPercentageText1.setText(zhuBoStatementMobile.getPercent() + "%");
                        binding.profitText1.setText(zhuBoStatementMobile.getMoney() + "元");
                        binding.dailyDiamondIncomeText1.setText(zhuBoStatementMobile.getDay_silver_wallet() +"");
                    }else if(statement == 1){//周
                        binding.weeklyLiveTime2.setText(zhuBoStatementMobile.getWeek_live_time());
                        binding.videoPublished2.setText(String.valueOf(zhuBoStatementMobile.getWeek_video_num()));
                        binding.percent2.setText(zhuBoStatementMobile.getPercent() +"%");
                        binding.rewardMethod2.setText(String.valueOf(zhuBoStatementMobile.getWeek_silver_wallet()));
                        binding.money2.setText(zhuBoStatementMobile.getMoney() +"元");
                    }else if(statement == 2){//月
                        //有效自然日
                        binding.validDate.setText(zhuBoStatementMobile.getLive_days() + "天");
                        binding.validDatePercentage.setText(zhuBoStatementMobile.getIs_live_days() + "%");
                        //平均日时长
                        binding.averageLiveTimeDaily.setText(zhuBoStatementMobile.getAvg_daytime() + "小时");
                        binding.averageLiveTimeDailyPercentage.setText(zhuBoStatementMobile.getIs_avg_daytime() + "%");
                        //视频发布量
                        binding.videoPublishQuantity.setText(zhuBoStatementMobile.getVideo_num() + "条");
                        binding.videoPublishQuantityPercentage.setText(zhuBoStatementMobile.getIs_video_num() + "%");
                        //新增有效用户量
                        binding.newUserIncrease.setText(zhuBoStatementMobile.getNew_user_num() + "人");
                        binding.newUserIncreasePercentage.setText(zhuBoStatementMobile.getIs_new_user_num() + "%");
                        binding.newUserIncreaseReward.setText(zhuBoStatementMobile.getNew_user_money() + "元");
                        //月累计时长
                        binding.liveTimeMonthly.setText(zhuBoStatementMobile.getTotal_time() + "时");
                        binding.liveTimeMonthlyPercentage.setText(zhuBoStatementMobile.getIs_total_time() + "%");
                        //合计奖励收益提点
                        binding.rewardPercentage.setText(zhuBoStatementMobile.getPercent() + "%");
                        //月钻石总数
                        binding.rewardForIntegrals.setText(String.valueOf(zhuBoStatementMobile.getSilver_wallet()));
                        //月奖励额
                        binding.rewardAmountForIntegrals.setText(zhuBoStatementMobile.getTotal_money() + "元");
                    }
                }
            }
        });

        //有效自然日
        binding.llNaturalDay.setOnClickListener(this);
        //平均日时长
        binding.llDayWhenLong.setOnClickListener(this);
        //视频发布量
        binding.llHairClothAmount.setOnClickListener(this);
        //月累计时长
        binding.llMeterWhenLong.setOnClickListener(this);
        //新增有效用户量
        binding.llEffectiveUser.setOnClickListener(this);
        //月流水详情
        binding.llDayFlowAter.setOnClickListener(this);
        binding.llWeekofwater.setOnClickListener(this);
        binding.llFlowWater.setOnClickListener(this);

    }

    @Override
    public void onSkipClicked(int position, Object mObject) {
        date = (String) mObject;
        if(!TextUtils.isEmpty(date)){
            if(statement == 0){
                zhuBoViewModel.AnchorDayReport(date);
            }else if(statement == 1){
                zhuBoViewModel.AnchorWeekReport(date);
            }else if(statement == 2){
                zhuBoViewModel.AnchorMonyhReport(date);
            }
            zhuBoStatementAdapter.setSelected(position);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_natural_day://有效自然日
                ToastUtils.make().setGravity(Gravity.CENTER,0,0).show("有效自然日达到25天,月收益提点+1%");
                break;
            case R.id.ll_day_when_long://平均日时长
                ToastUtils.make().setGravity(Gravity.CENTER,0,0).show("月平均日时长达到4H,月收益提点+1%");
                break;
            case R.id.ll_hair_cloth_amount://视频发布量
                ToastUtils.make().setGravity(Gravity.CENTER,0,0).show("有效视频发布量达到25条,月收益提点+1%");
                break;
            case R.id.ll_meter_when_long://月累计时长
                ToastUtils.make().setGravity(Gravity.CENTER,0,0).show("月累计时长过百,月收益提点+1%");
                break;
            case R.id.ll_effective_user://新增有效用户量
                ToastUtils.make().setGravity(Gravity.CENTER,0,0).show("新增有效用户量,月收益提点+1%");
                break;
            case R.id.ll_day_flow_ater://日流水详情
            case R.id.ll_Weekofwater://周流水详情
            case R.id.ll_flow_water://月流水详情
                if(statement == 0){
                    ARouter.getInstance().build(ARouterUtils.LiuShuiDetailsActivity)
                            .withString("date",date)
                            .withInt("distinction",0)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .navigation();
                }else if(statement == 1){
                    ARouter.getInstance().build(ARouterUtils.LiuShuiDetailsActivity)
                            .withString("date",date)
                            .withInt("distinction",1)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .navigation();
                }else if(statement == 2){
                    ARouter.getInstance().build(ARouterUtils.LiuShuiDetailsActivity)
                            .withString("date",date)
                            .withInt("distinction",2)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .navigation();
                }

                break;
        }
    }
}
